﻿using RentalIService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Newtonsoft.Json;

namespace RentalManagement.Controllers
{
    public class MainController : Controller
    {
        private readonly IServiceStaff role;
        private readonly IServiceRoleResourceModule roleModel;
        public MainController(IServiceStaff _role, IServiceRoleResourceModule _roleModel)
        {
            role = _role;
            roleModel = _roleModel;
        }
        // GET: Main
        [Authorize]
        public ActionResult Index()
        {
            ViewBag.li = DModel();

            if (Common.CookieHelper.ReadUser() == default)
            {
                ViewBag.name = "";
            }
            else
            {
                ViewBag.name = Common.CookieHelper.ReadUser().Name;
                ViewBag.id = Common.CookieHelper.ReadUser().Id;
            }

            //return Json(role.selectList(),JsonRequestBehavior.AllowGet);
            return View();
        }
        public string DModel()
        {
            var model = roleModel.selectRole(Common.CookieHelper.ReadUser().RoleId);
            string str = "";

            foreach (var a in model)
            {
                if (a.ParentId == "0")
                {
                    str += "<li class=\"dash\"><a href = \"" + a.Id + "\" title=\"\" class=\"exp\"><span>" + a.UrlName + "</span></a>";

                    var model1 = model.Where(u => u.ParentId == a.Id);
                    if (model1.Count() > 0)
                    {
                        str += "<ul class=\"sub\">";
                        foreach (var b in model1)
                        {
                            if (b.ParentId == a.Id)
                            {
                                str += " <li><a href =\" " + b.Url + " \" target=\"iframeMain\">" + b.UrlName + "</a></li>";
                            }
                        }
                        str += "</ul>";
                    }
                    str += "</li>";
                }
            }
            return str;

        }
        public ActionResult Error()
        {
            return View();
        }
        public ActionResult MainIndex()
        {
            return View();
        }
    }
}